package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.luo.model.*;
import com.luo.service.ClassService;
import org.aspectj.weaver.ast.Var;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 功能：根据二级id查询一二级分类信息
 * 作者：Luo。
 * 日期：2024/1/16 9:41
 */
@WebServlet("/category/sub/filter")
public class ListClassByOneClassIdController extends HttpServlet {

    ClassService classService = new ClassService();

    @Override
    protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        TwoClass twoClass = classService.queryTwoClassByTwoId(id);
        OneClass oneClass = classService.queryOneClassByOneId(twoClass.getOneClassId());
        OneTwoClass oneTwoClass = new OneTwoClass();
        oneTwoClass.setId(twoClass.getId());
        oneTwoClass.setName(twoClass.getName());
        oneTwoClass.setPicture(twoClass.getPicture());
        oneTwoClass.setParentId(oneClass.getId());
        oneTwoClass.setParentName(oneClass.getName());
        ResultObj result = new ResultObj();
        result.setCode("1");
        result.setMsg("操作成功");
        result.setResult(oneTwoClass);
        String jsonString = JSON.toJSONString(result);
        resp.getWriter().write(jsonString);
        resp.getWriter().close();
    }
}
